/**
 * 登录接口
 */
/**
 * @swagger
 *       /api/login:
 *       post:
 *           description: 用户登录接口
 *           tags: [用户登录]
 *           produces:
 *               - application/json
 *           parameters:
 *               - name: username
 *                 description: 用户名
 *                 required: true
 *                 type: string
 *               - name: password,
 *                 description: 密码
 *                 required: true
 *                 type: string
 *           responses:
 *               200:
 *                   description: 登录成功
 *               401:
 *                   description: 用户名或密码错误
 *               500:
 *                   description: 服务器错误
 */

var express = require("express");
var router = express.Router();
var sql = require('../db/index');
const { timeStamp } = require("console");
const { token } = require("morgan");

router.post('/login',async(req,res)=>{
    //console.log(req,'请求对象');
    //console.log(res,'响应对象');
    // 请求相关的对象 request
    // 响应相关的对象 
    //获取参数
    const {username,password}=req.body;
    const [result]= await sql.execute('select * from base_user where username= ? and password= ?',[username,password]);
    //console.log(result);
    if(result.length>0){
        res.send({
            status:200,
            message:'登录成功',
            date:result[0],//用户个人信息
            timestamp:new Date().getTime(),
            token:Math.random().toString(36)
        })
    }
    else{
        res.status(401).send('用户名或者密码错误')
    }
});

module.exports = router;